﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MedReg
{
    public partial class Employee : IEquatable<Employee>
    {
        public void ImportList(Classes.ArrayOfEmployee employeeList)
        {
            var mdb = new MedregDBDataContext();

            var sMar = new Classes.Marriage();
            var sCit = new Classes.Citizenship();
            var sTer = new Classes.Territory();

            var Mar = new Marriage();
            Mar.ImportList(sMar.getMarriageList(employeeList));

            var Cit = new Citizenship();
            Cit.ImportList(sCit.getCitizenshipList(employeeList));

            var Ter = new Territory();
            Ter.ImportList(sTer.getTerritoryList(employeeList));

            var sLpu = new Classes.LPU();
            var lpuImport = new LPU();
            sLpu.FixLPUList(employeeList);
            lpuImport.ImportList(sLpu.getLPUList(employeeList));
            
            var ListEmployee = new List<Employee>();
            foreach (var employee in employeeList)
            {
                Employee employee1 = new Employee()
                {
                    ID = employee.ID,
                    Population = employee.Population,
                    TabelNumber = employee.TabelNumber,
                    UZ = employee.UZ.ID,
                    RealUZ = employee.UZ.RealID,
                    ChangeTime = employee.ChangeTime,
                    Region = employee.Region.ID,
                    Name = employee.Name,
                    Surname = employee.Surname,
                    INN = employee.INN,
                    Patroname = employee.Patroname,
                    Sex = employee.Sex.ToString(),
                    Birthdate = employee.Birthdate, 
                    Deathdate = employee.Deathdate,
                    SNILS = employee.SNILS,
                    Phone = employee.Phone,
                    MarriageState = employee.MarriageState != null ?employee.MarriageState.ID: (int?) null,
                    CitizenshipState = employee.CitezenshipState != null ? employee.CitezenshipState.ID: (int?) null,
                    IsRealPerson = employee.IsRealPerson,
                    HasAuto = employee.HasAuto,
                    HasChildren = employee.HasChildren,
                    //Addresses = employee.Addresses,
                    //Document = employee.Document,
                    //EmployeeAwards = employee.EmployeeAwards,
                    //EmployeeRecords = employee.EmployeeRecords,
                    //EmployeeSpecialities = employee.EmployeeSpecialities,
                    //EmployeePostGraduateEducation = employee.EmployeePostGraduateEducation,
                    //EmployeeSertificateEducation = employee.EmployeeSertificateEducation,
                    //EmployeeSkillImprovement = employee.EmployeeSkillImprovement,
                    //EmployeeRetrainment = employee.EmployeeRetrainment,
                    //EmployeeQualification = employee.EmployeeQualification;
                };
               
                Employee query;
               

                if (mdb.Employee.LongCount() == 0)
                {
                    mdb.Employee.InsertOnSubmit(employee1);
                }
                else
                {
                    try
                    {
                        query = mdb.Employee.Single(l => l.ID == employee1.ID);
                        if (query.ChangeTime.Date < employee1.ChangeTime.Date)
                        {
                            query.Population = employee.Population;
                            query.TabelNumber = employee.TabelNumber;
                            query.UZ = employee.UZ.ID;
                            query.RealUZ = employee.UZ.RealID;
                            query.ChangeTime = employee.ChangeTime;
                            query.Region = employee.Region.ID;
                            query.Name = employee.Name;
                            query.Surname = employee.Surname;
                            query.INN = employee.INN;
                            query.Patroname = employee.Patroname;
                            query.Sex = employee.Sex.ToString();
                            query.Birthdate = employee.Birthdate;
                            query.Deathdate = employee.Deathdate;
                            query.SNILS = employee.SNILS;
                            query.Phone = employee.Phone;
                            query.MarriageState = employee.MarriageState.ID;
                            query.CitizenshipState = employee.CitezenshipState.ID;
                            query.IsRealPerson = employee.IsRealPerson;
                            query.HasAuto = employee.HasAuto;
                            query.HasChildren = employee.HasChildren;
                            //query.Document = employee.Document;
                            //query.Addresses = employee.Addresses;
                            //query.EmployeeAwards = employee.EmployeeAwards;
                            //query.EmployeeRecords = employee.EmployeeRecords;
                            //query.EmployeeSpecialities = employee.EmployeeSpecialities;
                            //query.EmployeePostGraduateEducation = employee.EmployeePostGraduateEducation;
                            //query.EmployeeSertificateEducation = employee.EmployeeSertificateEducation;
                            //query.EmployeeSkillImprovement = employee.EmployeeSkillImprovement;
                            //query.EmployeeRetrainment = employee.EmployeeRetrainment;
                            //query.EmployeeQualification = employee.EmployeeQualification;

                           
                        }

                    }
                    catch (ArgumentNullException e)
                    {
                    }
                    catch (InvalidOperationException e)
                    {
                        if (!ListEmployee.Contains(employee1))
                        {
                            mdb.Employee.InsertOnSubmit(employee1);
                            ListEmployee.Add(employee1);
                        }
                        mdb.Employee.InsertOnSubmit(employee1);
                    }
                }
            }

            mdb.SubmitChanges();

            var sPostGrEd = new Classes.PostGraduateEducation();
            var PostGrEd = new PostGraduateEducation();
            PostGrEd.ImportList(sPostGrEd.getPostGraduateEducationList(employeeList));

            var awards = new Classes.Award();
            var sAwards = new Award();
            sAwards.ImportList(awards.getAwardList(employeeList));

            

            var sCardRecord = new Classes.CardRecord();
            var CardRecord = new CardRecord();
            CardRecord.ImportList(sCardRecord.getCardRecord(employeeList));

            var sAddressEntity = new Classes.AddressEntity();
            var AddressEntity = new AddressEntity();
            AddressEntity.ImportList(sAddressEntity.getAddressEntity(employeeList));

            var sDocumentEntity = new Classes.DocumentEntity();
            var DocumentEntity = new DocumentEntity();
            DocumentEntity.ImportList(sDocumentEntity.getDocumentEntityList(employeeList));

            var sRetr = new Classes.Retrainment();
            var Retr = new Retrainment();
            Retr.ImportList(sRetr.getRetrainmentList(employeeList));

            var sDipEd = new Classes.DiplomaEducation();
            var DipEd = new DiplomaEducation();
            DipEd.ImportList(sDipEd.getDiplomaEducationList(employeeList));

            var sSkillIm = new Classes.SkillImprovement();
            var SkillIm = new SkillImprovement();
            SkillIm.ImportList(sSkillIm.getSkillImprovementListList(employeeList));

            var sSerEd = new Classes.SertificateEducation();
            var SerEd = new SertificateEducation();
            SerEd.ImportList(sSerEd.getSertificateEducationList(employeeList));

            var sQual = new Classes.Qualification();
            var Qual = new Qualification();
            Qual.ImportList(sQual.getQualificationList(employeeList));

           

        }
        public bool Equals(Employee other)
        {
            var i = this.ID.Equals(other.ID);
            var j = this.Name.Equals(other.Name);
            var k = this.SNILS.Equals(other.SNILS);
            if (i & j & k)
            {
                return true;
            }
            return false;
        }
        public List<Employee> GetEmployeeList(Guid lpuID)
        {
            return (new MedregDBDataContext()).Employee.Where(e => e.UZ.Equals(lpuID)).ToList();
        }
    }
}
